﻿
 /**
  * 创建普通地板
  * @param {any} name  名称  floor
  * @param {any} size  尺寸 {x:8000,z:5000,y:100} 长 宽 厚
  * @param {any} CenterPosition 中心点位置  {x:0,z:-60,y:0}
  * @param {any} rotation 旋转  {x:0,y:0,z:0} 角度用
  */
function createFloor(name, size, CenterPosition, rotation) {
    if (!rotation) {
        rotation = { x: 0, y: 0, z: 0 }
    } else {
        rotation.x = rotation.x / 180 * Math.PI;
        rotation.y = rotation.y / 180 * Math.PI;
        rotation.z = rotation.z / 180 * Math.PI;
    }
    var model = {
        "show": true,
        "uuid": "",
        "name": name,
        "objType": "cube2",
        "length": size.x+40,
        "width": size.z + 40,
        "height": size.y,
        "x": CenterPosition.x,
        "y": CenterPosition.y,
        "z": CenterPosition.z,
        "style": {
            "skinColor": 16777215,
            "skin": {
                "skin_up": {
                    "skinColor": 16383743,
                    "materialType": "lambert",
                    "side": 1, "opacity": 1,
                    "imgurl": "../img/3dImg/floor4.jpg",
                    "repeatx": true, "width": size.x/400,
                    "repeaty": true, "height": size.z / 250
                }, "skin_down": { "skinColor": 11119789, "side": 1, "opacity": 1 },
                "skin_fore": { "skinColor": 11119789, "side": 1, "opacity": 1 },
                "skin_behind": { "skinColor": 11119789, "side": 1, "opacity": 1 },
                "skin_left": { "skinColor": 11119789, "side": 1, "opacity": 1 },
                "skin_right": { "skinColor": 11119789, "side": 1, "opacity": 1 }
            }
        },
        "showSortNub": 28,
        "customType1": "",
        "customType2": "",
        "animation": null,
        "dbclickEvents": null,
        "rotation": [
            { "direction": "x", "degree": rotation.x },
            { "direction": "y", "degree": rotation.y },
            { "direction": "z", "degree": rotation.z }
        ],
        "thick": null,
        "scale": { "x": 1, "y": 1, "z": 1 },
        "BindDevId": null,
        "BindDevName": null,
        "devInfo": null,
        "BindMeteId": null,
        "BindMeteName": null
    };
    return model;
}
/**
 * 创建墙体
 * @param {any} name 名称 wall_4
 * @param {any} size 尺寸{x:8000,z:100,y:1000} 长 厚 高
 * @param {any} startPosition 起始点位置 { "x": -4858.313, "y": 0, "z": 1264.35 }
 * @param {any} doorHoles 门洞  [
                                    {
                                        type:2 ,//0 x方向单门 1 x反方向单门 2 双门 3窗户
                                        start: 400,//相对于尺寸参数的x 离0多长 比如400 表示门洞离x 0点400
                                        width:600,//门洞宽
                                        height:700,//门洞高
                                        thickness:40,//门洞厚度
                                        rideHeight:100//离地高度
                                    }

                                ]

 * @param {any} rotation 旋转  {x:0,y:0,z:0}
 */
function createWall(name,type, size, startPosition, doorHoles, rotation) {
    if (!doorHoles) {
        doorHoles = [];
    }
    if (!rotation) {
        rotation = { x: 0, y: 0, z: 0 }
    } else {
        rotation.x = rotation.x / 180 * Math.PI;
        rotation.y = rotation.y / 180 * Math.PI;
        rotation.z = rotation.z / 180 * Math.PI;
    }
    var holes = [];
    var doors = []; 
    $.each(doorHoles, function (_index, _obj) {
        var _rideHeight = 0.1;
        if (_obj.rideHeight) {
            _rideHeight = _obj.rideHeight;
        }
        holes.push([
            { "x": _obj.start, "y": _rideHeight, "type": "nomal" },
            { "x": (_obj.width + _obj.start), "y": _rideHeight, "type": "nomal" },
            { "x": (_obj.width + _obj.start), "y": _rideHeight+_obj.height, "type": "nomal" },
            { "x": _obj.start, "y": _rideHeight+_obj.height, "type": "nomal" }]);
        if (_obj.type == 0 || _obj.type==2) {
      
        doors.push({
            "show": true,
            "uuid": "",
            "name": name + "OBJCREN_leftDoor_" + _index,
            "objType": "ExtrudeGeometry",
            "position": { "x": _obj.start, "y": _rideHeight, "z": ((size.z - _obj.thickness) / 2) },
            "style": {
                "skinColor": 16711680,
                "skin": {
                    "skin_up": {
                        "skinColor": 9676206,
                        "side": 1,
                        "opacity": 1,
                        "imgurl": "../img/3dImg/doorleft.jpg",
                        "repeatx": true, "width": 0.0034 / ((_obj.width / 600) / (_obj.type == 2 ? 1 : 0.5)),
                        "repeaty": true, "height": 0.0014 / (_obj.height / 700),
                    },
                    "skin_down":{ "skinColor": 16711680, "side": 1, "opacity": 1 },
                    "skin_side":{ "skinColor": 9676206, "opacity": 1 }
                }
            },
            "scale": { "x": 1, "y": 1, "z": 1 },
            "shapeParm": {
                "points": [{ "x": 0, "y": 0, "type": "nomal" },
                    { "x": 0, "y": _obj.height, "type": "nomal" },
                    { "x": (_obj.type == 2 ? (_obj.width / 2) : (_obj.width)), "y": _obj.height, "type": "nomal" },
                    { "x": (_obj.type == 2 ? (_obj.width / 2) : (_obj.width)), "y": 0, "type": "nomal" }],
                "holes": []
            }, 
            "extrudeSettings":
            {
                "amount": _obj.thickness,
                "curveSegments": 1,
                "steps": 1, "bevelEnabled": false, "bevelThickness": 1, "bevelSize": 1, "bevelSegments": 1, "extrudePathPoints": []
            },
            "showSortNub": 7, "customType1": "", "customType2": "", "animation": null, "dbclickEvents": null, "rotation": [
                { "direction": "x", "degree": 0 },
                { "direction": "y", "degree": 0 },
                { "direction": "z", "degree": 0 }],
            "BindDevId": null,
            "BindDevName": null,
            "devInfo": null,
            "BindMeteId": null,
            "BindMeteName": null
            });
        }
        if (_obj.type ==1 || _obj.type == 2) {

            doors.push({
                "show": true,
                "uuid": "",
                "name": name + "OBJCREN_rightDoor_" + _index,
                "objType": "ExtrudeGeometry",
                "position": { "x": (_obj.width + _obj.start), "y": _rideHeight, "z": ((size.z - _obj.thickness) / 2) },
                "style": {
                    "skinColor": 16711680,
                    "skin": {
                        "skin_up":
                        {
                            "skinColor": 9676206, "side": 1, "opacity": 1,
                            "imgurl": "../img/3dImg/doorright.jpg",
                            "repeatx": true, "width": 0.0034 / ((_obj.width / 600) / (_obj.type == 2 ? 1 : .5)),
                            "repeaty": true, "height": 0.0014 / (_obj.height / 700),
                        },
                        "skin_down": { "skinColor": 16711680, "side": 1, "opacity": 1 },
                        "skin_side": { "skinColor": 9676206, "opacity": 1 }
                    }
                },
                "scale": { "x": 1, "y": 1, "z": 1 },
                "shapeParm": {
                    "points":
                    [
                        { "x": 0, "y": 0, "type": "nomal" },
                    { "x": 0, "y": _obj.height, "type": "nomal" },
                    { "x": (_obj.type == 2 ? (-_obj.width / 2) : (-_obj.width)) , "y": _obj.height, "type": "nomal" },
                    { "x": (_obj.type == 2 ? (-_obj.width / 2) : (-_obj.width)), "y": 0, "type": "nomal" }],
                    "holes": []
                },
                "extrudeSettings":
                {
                    "amount": _obj.thickness,
                    "curveSegments": 1,
                    "steps": 1,
                    "bevelEnabled": false,
                    "bevelThickness": 1,
                    "bevelSize": 1,
                    "bevelSegments": 1,
                    "extrudePathPoints": []
                }, "showSortNub": 7,
                "customType1": "",
                "customType2": "",
                "animation": null,
                "dbclickEvents": null,
                "rotation": [{ "direction": "x", "degree": 0 }, { "direction": "y", "degree": 0 }, { "direction": "z", "degree": 0 }],
                "BindDevId": null, "BindDevName": null, "devInfo": null, "BindMeteId": null, "BindMeteName": null
            });
        }
        if (_obj.type == 3) {

            doors.push({
                "show": true,
                "uuid": "",
                "name": name + "OBJCREN_window_" + _index,
                "objType": "ExtrudeGeometry",
                "position": { "x": _obj.start, "y": _rideHeight, "z": ((size.z - _obj.thickness) / 2) },
                "style": {
                    "skinColor": 16711680,
                    "skin": {
                        "skin_up": {
                            "skinColor": 9676206,
                            "side": 1,
                            "opacity": 1,
                            "imgurl": "../img/3dImg/wall/glass2.png",
                            "repeatx": true, "width": 0.02 * (parseInt(_obj.width / _obj.height)+1) / (_obj.width / 100),
                            "repeaty": true, "height": 0.01 / (_obj.height / 100),
                        },
                        "skin_down": { "skinColor": 16711680, "side": 1, "opacity": 1 },
                        "skin_side": { "skinColor": 9676206, "opacity": 1 }
                    }
                },
                "scale": { "x": 1, "y": 1, "z": 1 },
                "shapeParm": {
                    "points": [{ "x": 0, "y": 0, "type": "nomal" },
                    { "x": 0, "y": _obj.height, "type": "nomal" },
                    { "x": _obj.width, "y": _obj.height, "type": "nomal" },
                    { "x": _obj.width, "y": 0, "type": "nomal" }],
                    "holes": []
                },
                "extrudeSettings":
                {
                    "amount": _obj.thickness,
                    "curveSegments": 1,
                    "steps": 1, "bevelEnabled": false, "bevelThickness": 1, "bevelSize": 1, "bevelSegments": 1, "extrudePathPoints": []
                },
                "showSortNub": 1001, "customType1": "", "customType2": "", "animation": null, "dbclickEvents": null, "rotation": [
                    { "direction": "x", "degree": 0 },
                    { "direction": "y", "degree": 0 },
                    { "direction": "z", "degree": 0 }],
                "BindDevId": null,
                "BindDevName": null,
                "devInfo": null,
                "BindMeteId": null,
                "BindMeteName": null
            });
        }
    });
    var style = {
        "skinColor": 16711680,
        "skin":
        {
            "skin_up": { "skinColor": 16777215, "side": 1, "opacity": 1, "imgurl": "../img/3dImg/inside_lightmap.jpg", "repeatx": true, "width": 0.01, "repeaty": true, "height": 0.001 / (size.y / 1000) },
            "skin_down": { "skinColor": 16711680, "side": 1, "opacity": 1 },
            "skin_side": { "skinColor": 15724527, "opacity": 1 }
        }
    };
    if (type == "glassWall") {
        style ={
            "skinColor": 16711680,
                "skin":
            {
                    "skin_up": { "skinColor": 16777215, "side": 1, "opacity": 1, "imgurl": "../img/3dImg/wall/glass-wall4.png", "repeatx": true, "width": 0.001, "repeaty": true, "height": 0.001 / (size.y / 1000) },
                "skin_down": { "skinColor": 16711680, "side": 1, "opacity": 1 },
                "skin_side": { "skinColor": 15724527, "opacity": 0.1 }
            }
        };
    }
    var model = {
        "show": true,
        "uuid": "",
        "name": name,
        "objType": "GroupObj",
        "scale": { "x": 1, "y": 1, "z": 1 },
        "position": { "x": startPosition.x, "y": startPosition.y, "z": startPosition.z },
        "rotation": [
            { "direction": "x", "degree": rotation.x },
            { "direction": "y", "degree": rotation.y },
            { "direction": "z", "degree": rotation.z }
        ],
        "childrens": [
            {
                "show": true, "uuid": "",
                "name": name+"OBJCREN_wall",
                "objType": "ExtrudeGeometry",
                "position": { "x": 0, "y": 0, "z": 0 },
                "style": style,
                "scale": { "x": 1, "y": 1, "z": 1 },
                "shapeParm":
                {
                    "points": [
                        { "x": 0, "y": 0, "type": "nomal" },
                        { "x": 0, "y": size.y, "type": "nomal" },
                        { "x": size.x, "y": size.y, "type": "nomal" },
                        { "x": size.x, "y": 0, "type": "nomal" }
                    ],
                    "holes": holes
                  
                },
                "extrudeSettings": {
                    "amount": size.z,
                    "curveSegments": 1,
                    "steps": 1,
                    "bevelEnabled": false,
                    "bevelThickness": 1,
                    "bevelSize": 1,
                    "bevelSegments": 1,
                    "extrudePathPoints": []
                },
                "showSortNub": (type == "glassWall" ? 10000:100) ,
                "customType1": "",
                "customType2": "",
                "animation": null,
                "dbclickEvents": null,
                "rotation": [{ "direction": "x", "degree": 0 }, { "direction": "y", "degree": 0 }, { "direction": "z", "degree": 0 }],
                "BindDevId": null,
                "BindDevName": null,
                "devInfo": null,
                "BindMeteId": null,
                "BindMeteName": null
            }],
        "showSortNub": 6, "customType1": "", "customType2": "", "animation": null, "dbclickEvents": null, "BindDevId": null, "BindDevName": null, "devInfo": null, "BindMeteId": null, "BindMeteName": null
    };
    model.childrens = model.childrens.concat(doors);
    return model;
}
function CreateRoomByJson(configjson, baseConfig) {
    var scaleRate = 1;
    if (baseConfig && baseConfig.normRackSize) {
        scaleRate = baseConfig.normRackSize.width / 350;
    }
    var roomModels = [{ "show": true, "uuid": "", "name": "light", "objType": "DirectionalLight", "shadowCameraNear": 1, "shadowCameraFar": 10240, "shadowCameraLeft": -5120, "shadowCameraRight": 5120, "shadowCameraTop": 5120, "shadowCameraBottom": -5120, "shadowMapWidth": 2048, "shadowMapHeight": 2048, "distance": 1, "targetName": "", "intensity": 1, "color": 3094843, "castShadow": true, "position": { "x": (baseConfig.centerx / 2)/ scaleRate, "y": 4000, "z": (baseConfig.centerz /2) / scaleRate}, "showSortNub": 8, "customType1": "", "customType2": "", "animation": null, "dbclickEvents": null, "shadowCameraFov": null, "BindDevId": null, "BindDevName": null, "devInfo": null, "BindMeteId": null, "BindMeteName": null }];
    $.each(configjson, function (_index, _obj) {
        var modeljson = null;
        console.log('_obj.size',_obj.size);
        switch (_obj.type) {
            case "floor":
                modeljson = createFloor(_obj.name, _obj.size, _obj.CenterPosition, _obj.rotation);
                break;
            case "wall":
                modeljson = createWall(_obj.name, "wall",_obj.size, _obj.startPosition, _obj.doorHoles, _obj.rotation);
                break;
            case "glassWall":
                modeljson = createWall(_obj.name, "glassWall", _obj.size, _obj.startPosition, _obj.doorHoles, _obj.rotation);
                break;
        }

        if (modeljson) {
            roomModels.push(modeljson);
        }
    });

    var buildwall = [{
        "show": true,
        "uuid": "",
        "name": "roombuild",
        "objType": "GroupObj",
        "scale": { "x": 1 / scaleRate, "y": 1 / scaleRate, "z": 1 / scaleRate },
        "position": { "x":0, "y":0, "z":0 },
        "rotation": [
            { "direction": "x", "degree": 0 },
            { "direction": "y", "degree": 0},
            { "direction": "z", "degree": 0 }
        ],
        "childrens": roomModels,
        "showSortNub": 6, "customType1": "", "customType2": "", "animation": null, "dbclickEvents": null, "BindDevId": null, "BindDevName": null, "devInfo": null, "BindMeteId": null, "BindMeteName": null
    }]
    return buildwall;
}